<?php

namespace App\Http\Controllers\Admin;

use Admin;
use App\admin\src\Components\Callbacks\UpdateUserCallback;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Largezhou\Admin\Form\Form;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    public function index(Request $request, User $user)
    {
        $content = Admin::content()
                        ->title('用户')
                        ->desc('用户列表')
                        ->content($this->table());

        return response()->json([
            'status'  => true,
            'content' => $content,
        ]);
    }

    protected function table()
    {
        $table = Admin::table(User::class)
                      ->columns([
                          'id'         => '编号',
                          'avatar'     => '头像',
                          'name'       => '用户名',
                          'email'      => '邮箱',
                          'created_at' => '添加时间',
                          'updated_at' => '最后更新',
                      ]);

        return $table;
    }

    public function create(Request $request, User $user)
    {
        $content = Admin::content()
                        ->title('用户')
                        ->desc('添加用户')
                        ->content($this->form($user));

        return response()->json([
            'status'  => true,
            'content' => $content,
        ]);
    }

    public function edit(Request $request, User $user)
    {
        $content = Admin::content()
                        ->title('用户')
                        ->desc('编辑用户')
                        ->content($this->form($user)->edit());

        return response()->json([
            'status'  => true,
            'content' => $content,
        ]);
    }

    protected function form(User $user = null)
    {
        $form = Admin::form($user);

        $form->input('name', '用户名')->required();
        $form->upload('avatar', '头像')
             ->accept('image/*')
             ->limit(1)
             ->dir('avatars');
        $form->input('email', '邮箱')->required();

        $form->input('password', '密码')->props('type', 'password')->required();
        $form->input('password_confirmation', '确认密码')->setDefault(function (Form $form) {
            return array_get($form->getData(), 'password');
        })->props('type', 'password')->required();

        $form->multipleSelect('roles', '角色')
             ->options(Role::pluck('display_name', 'id')->toArray())
             ->listStyle('h')
             ->placeholder('选择角色');

        $form->saving(function (Form $form) {
            $newPW = $form->inputs('password');
            if ($newPW && $form->model()->password != $newPW) {
                $form->inputs('password', bcrypt($newPW));
            }
        });

        if (
            $user &&
            $user->id === auth('api')->id()
        ) {
            $form->saved(function () use ($user) {
                return response()->json([
                    'status'   => true,
                    'msg'      => '更新成功',
                    'to'       => route_path(-1),
                    'callback' => new UpdateUserCallback([
                        'name'   => $user->name,
                        'avatar' => $user->avatar,
                    ]),
                ]);
            });
        }

        return $form;
    }

    public function store(UserRequest $request, User $user)
    {
        return $this->form($user)->store();
    }

    public function destroy(User $user)
    {
        return $this->form($user)->destroy();
    }

    public function update(UserRequest $request, User $user)
    {
        return $this->form($user)->update();
    }
}
